import { Collect,CreateInvitCode,Recharge,UpdatePassword,UpdatePoints } from '@/services/member';
import { Reducer, Effect } from 'umi';

export interface ModelType {
    namespace: string;
    state: any;
    effects: {
        collect: Effect;
        createInvitCode: Effect;        
        recharge: Effect;
        updatePassword: Effect;
        updatePoints: Effect;
        setCurrent: Effect;
    };
    reducers: {
        deal: Reducer<any>;
        set: Reducer<any>;
    };
}

const MemberModel: ModelType = {
    namespace: 'Member',
    state: {
        current: {},
        response: {},
    },
    effects: {
        * collect({ payload }, { call, put }) {
            const response = yield call(Collect, payload);
            yield put({
                type: 'deal',
                payload: response,
            });
        },
        * createInvitCode({ payload }, { call, put }) {
            const response = yield call(CreateInvitCode, payload);
            yield put({
                type: 'deal',
                payload: response,
            });
        },
        * recharge({ payload }, { call, put }) {
            const response = yield call(Recharge, payload);
            yield put({
                type: 'deal',
                payload: response,
            });
        },
        * updatePassword({ payload }, { call, put }) {
            const response = yield call(UpdatePassword, payload);
            yield put({
                type: 'deal',
                payload: response,
            });
        },
        * updatePoints({ payload }, { call, put }) {
            const response = yield call(UpdatePoints, payload);
            yield put({
                type: 'deal',
                payload: response,
            });
        },
        * setCurrent({ payload }, { put }) {
            yield put({
                type: 'set',
                payload,
            });
        },
    },
    reducers: {
        set(state, action) {
            return { ...state, current: action.payload };
        },
        deal(state, action) {
            return { ...state, response: action.payload };
        },
    },
};
export default MemberModel;